<?php
define("APPID", "wx6d216db159fec225");
define("APPSECRET", "b1faa3493802bf4175d41bb463454bb5");
function getAccessToken(){
        $access_token_from_file=file_get_contents('access_token');
        $access_token_time_from_file=file_get_contents('access_token_time');
        $now=time();
        if ($now-$access_token_time_from_file>7200) {
             $access_token_api="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
                $access_token=http_get($access_token_api);
                $data=json_decode($access_token);
                file_put_contents('access_token', $data->access_token);
                file_put_contents('access_token_time',time());
                return $data->access_token;
        }else{
            return $access_token_from_file;
        }
    }
    function getJsApiTicket(){
        $jsapi_ticket_from_file=file_get_contents('jsapi_ticket');
        $jsapi_ticket_time_from_file=file_get_contents('jsapi_ticket_time');
        $now=time();
        if ($now-$jsapi_ticket_time_from_file>7200) {
             $jsapi_ticket_api="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".getAccessToken()."&type=jsapi";
                $jsapi_ticket=http_get($jsapi_ticket_api);
                $data=json_decode($jsapi_ticket);
                file_put_contents('jsapi_ticket', $data->ticket);
                file_put_contents('jsapi_ticket_time',time());
                return $data->ticket;
        }else{
            return $jsapi_ticket_from_file;
        }
    }
function http_get($url){
		$oCurl = curl_init();
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
		}
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
	}	 
/**
	 * POST 请求
	 * @param string $url
	 * @param array $param
	 * @param boolean $post_file 是否文件上传
	 * @return string content
	 */
	 function http_post($url,$param,$post_file=false){
		$oCurl = curl_init();
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
		}
	        if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {
	            	$is_curlFile = true;
	        } else {
	        	$is_curlFile = false;
	            	if (defined('CURLOPT_SAFE_UPLOAD')) {
	                	curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
	            	}
	        }
		if (is_string($param)) {
	            	$strPOST = $param;
	        }elseif($post_file) {
	            	if($is_curlFile) {
		                foreach ($param as $key => $val) {
		                    	if (substr($val, 0, 1) == '@') {
		                        	$param[$key] = new \CURLFile(realpath(substr($val,1)));
		                    	}
		                }
	            	}
			$strPOST = $param;
		} else {
			$aPOST = array();
			foreach($param as $key=>$val){
				$aPOST[] = $key."=".urlencode($val);
			}
			$strPOST =  join("&", $aPOST);
		}
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt($oCurl, CURLOPT_POST,true);
		curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
	}
	function createNonceStr(){
		$str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		$re='';
		for ($i=0;$i<13;$i++) { 
			$index=rand(0,strlen($str)-1);
			$re .=$str[$index];
		}
		return $re;
	}	